package guestbook;
import javax.servlet.http.*;

import com.google.appengine.api.datastore.Text;

/*
 * this class will run based on the cron jobs. it fetches the toto
 * Recommendation information and stores it in the DB
 */

public class gettingTotoRec extends HttpServlet 
{    
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	 {
		try 
		{
			String url="http://www.dapper.net/RunDapp?dappName=Liga_Recommendations_score_and_verbal&v=1&applyToUrl=http://www.liga.co.il/recomview.php%3Fcat%3D36%26recomID%3D1084";
			String field=null;
			String[] result=null;
			String[] score= new String[16];
			Text[] verbal= new Text[16];
			boolean flag=true;
		
			totoRec form=new totoRec(score,verbal);
				
			/* this loop gets the data for all relevant fields*/
			for (int i=1;i<3;i++){
				switch (i) {
					case 1: field="Liga_recommendations_score_only";break;
					case 2: field="Liga_Recommendations_verbal_only";break;				
				}
				
				result=parsingSites.getDataFromXml(result, field, url);
				
				/* if the result is null, it means a dapper approach failed*/
				if (result==null){
					resp.getWriter().println("error");
					flag=false;
					return;
				}			
			
				switch (i) {
					case 1: form.setScore(result);break;
					case 2: form.setVerbal(result);break;
				}
			/* when the code above is complete, we have a form object ready for DB*/
			}
			
			
				
		/* the code below stores the form object in the DB, for quicker approach
		  if the flag is true, it means result was never null*/
			
			DBMethods.saveInDB(form,flag);
			
			/* returns success to caller if all is well*/
			resp.setContentType("text/plain;charset=UTF-8");
			resp.getWriter().println("success");
		
		} catch (Exception e)
		{
		e.printStackTrace();
		}
	}
			
}
